package com.jason.account.service;


import com.jason.account.dao.TccAccountMapper;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class AccountService implements IAccountService{
    Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    TccAccountMapper tccAccountMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean prepare(BusinessActionContext businessActionContext,Integer accountId, Integer amount) {
        logger.info("执行prepare");
        tccAccountMapper.updateAmountById(amount,accountId);
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean commit(BusinessActionContext businessActionContext) {
        logger.info("执行commit");
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean rollback(BusinessActionContext businessActionContext) {
        logger.info("执行rollback");
        Integer amount =  businessActionContext.getActionContext("amount",Integer.class);
        Integer accountId =  businessActionContext.getActionContext("accountId",Integer.class);
        tccAccountMapper.updateAmountById(-amount,accountId);
        return true;
    }

}
